/*!
 *
 * \file    MxdCmmbDemuxer.h
 *
 * \brief    Demuxer Interface header file.
 *
 * Header file of the functions declaration about MIRA CMMB Demuxer
 *
 * \par    Include files
 * - MxdCmmbDemuxer.h
 *
 * \par    Copyright (c) 2008 Maxscend Technologies Inc. All rights reserved
 *
 * PROPRIETARY RIGHTS of Maxscend Technologies Inc. are involved in
 * the subject matter of this material.  All manufacturing, reproduction,
 * use, and sales rights pertaining to this subject matter are governed
 * by the license agreement.  The recipient of this software implicitly
 * accepts the terms of the license.
 *
 * \version
 * Revision of last commit: $Rev: 818 $
 * Author of last commit  : $Author: maxscend\chaohui.jiang $
 * Date of last commit    : $Date: 2009-10-19 18:00:50 +0800 (星期一, 19 十月 2009) $
 *
 */

#ifndef __MXD_CMMB_DEMUXER_H_
#define __MXD_CMMB_DEMUXER_H_

#include "Mxd0250API.h"

#ifdef __cplusplus
extern "C" {
#endif

/************************* Function Definition ****************************/

/*******************************************************************************
 * \par Description
 * This function is used to Register the Callbacks to demuxer, and demuxer will create private struct in the handle. 
 *
 * \param ;
 *
 * \return   
 *    MXD_RTN_OK	-	the call succeeded
 *    Others	-	the demuxer initialization failed.
 *
 * \warning    
 */
MXD_RETURN_CODE DMX_Initialize( MXD_HANDLE hDevice );

/*******************************************************************************
 * \par Description
 * This function is used to release the resource allocated by demuxer. 
 *
 * \param 
 *
 * \return   
 *    MXD_RTN_OK	-	the call succeeded
 *    Others	-	the demuxer destory failed.
 *
 * \warning    
 */
MXD_RETURN_CODE DMX_Destory( MXD_HANDLE hDevice );

/*******************************************************************************
 * \par Description
 * This function is used to reset the demuxer, it must be called after frequency changed.
 *
 * \param 
 *
 * \return   
 *    MXD_RTN_OK	-	the call succeeded
 *    Others	-	the Device reset failed.
 *
 * \warning    
 */
MXD_RETURN_CODE DMX_Reset( MXD_HANDLE hDevice, MXD_CMMBSERVICE_INFO *pSavedSeviceChannelList, MXD_UINT32 service_count );

/*******************************************************************************
 * \par Description
 * This function is used to check if the frame is valid CLCH frame. 
 *
 * \param data	[IN] frame data;
 * \param len	[IN] frame length;
 *
 * \return   
 *    MXD_RTN_OK	-  the frame is valid
 *
 * \warning    
 */
MXD_RETURN_CODE DMX_VerifyControlFrame( MXD_HANDLE hDevice, MXD_UINT8* data, MXD_INT len );

/*******************************************************************************
 * \par Description
 * This function is used to feed the CLCH frame data and get out the service list.
 * Notice: the service name information must demux from esg data, this function will return an empty name.
 *
 * \param data	[IN] CLCH frame data;
 * \param len	[IN] CLCH frame length;
 * \param ppinfo [IN] service info list, will be malloced by demuxer;
 *
 * \return   
 *    MXD_RTN_OK	-  the service name is filled.
 *    MXD_DEMUXER_CONTINUE - demuxer have not get enough data from current ESG data, caller must feed more data.
 *    other - demuxer fail.
 *
 * \warning    
 */
MXD_RETURN_CODE DMX_GetServiceInfoList( MXD_HANDLE hDevice, MXD_UINT8* data, MXD_INT len, MXD_CMMBSERVICE_INFO **ppinfo, MXD_UINT32* pservice_count );

/*******************************************************************************
 * \par Description
 * This function is used to pass in service list which saved by host.
 *
 * \param pinfo [IN] service info list;
 * \param service_count	[IN] service count;
 *
 * \return   
 *    MXD_RTN_OK	-  the service name is filled.
 *    other - demuxer fail.
 *
 * \warning    
 */
MXD_RETURN_CODE DMX_SetServiceInfoList( MXD_HANDLE hDevice, MXD_CMMBSERVICE_INFO *pinfo, MXD_UINT32 service_count );

/*******************************************************************************
 * \par Description
 * This function is used to feed the ESG frame data and demux will fill the service name to the list.
 * Notice: current ESG frame data may have not enough information for demuxer to find out the service names. 
 *      caller must feed more data when this function return MXD_DEMUXER_CONTINUE.
 *
 * \param data	[IN] ESG frame data;
 * \param len	[IN] ESG frame length;
 * \param ppinfo [IN] service info list, will be malloced by demuxer;
 *
 * \return   
 *    MXD_RTN_OK	-  the service name is filled.
 *    MXD_DEMUXER_CONTINUE - demuxer have not get enough data from current ESG data, caller must feed more data.
 *    MXD_DEMUXER_NOTFOUND - demuxer have received duplicated data but not found the information for service name.
 *
 * \warning    
 */
MXD_RETURN_CODE DMX_GetServiceNameFromEsg( MXD_HANDLE hDevice, MXD_UINT8* data, MXD_INT len, MXD_CMMBSERVICE_INFO **ppinfo, MXD_UINT32* pservice_count );

/*******************************************************************************
 * \par Description
 * This function is used to register the user parameter to demuxer.
 *
 * \param serviceID	[IN] service id; if the user param is for channel search, this is must be 0;
 * \param userParam	[IN] user parameter;
 *
 * \return   
 *    MXD_RTN_OK	-  the service parameter is filled.
 *
 * \warning    
 */
MXD_RETURN_CODE DMX_RegisterServiceUserParam( MXD_HANDLE hDevice, MXD_UINT32 serviceID, void* userParam );

/*******************************************************************************
 * \par Description
 * This function is used to unregister the user parameter from demuxer.
 *
 * \param serviceID	[IN] service id; if the user param is for channel search, this is must be 0;
 *
 * \return   
 *    MXD_RTN_OK	-  the service paremeter is deleted.
 *
 * \warning    
 */
MXD_RETURN_CODE DMX_UnregisterServiceUserParam( MXD_HANDLE hDevice, MXD_UINT32 serviceID );

/*******************************************************************************
 * \par Description
 * This function is used to feed the CLCH frame data then demuxer will check if there is emergency message in it.
 *    If demuxer found an emergency message, the OnEmergencyMsg will called by demuxer.
 *
 * \param data	[IN] CLCH frame data;
 * \param len	[IN] CLCH frame length;
 *
 * \return   
 *    MXD_RTN_OK	-  demux ok.
 *
 * \warning    
 */
MXD_RETURN_CODE DMX_FeedControlFrameData( MXD_HANDLE hDevice, MXD_UINT8* data, MXD_INT len );

/*******************************************************************************
 * \par Description
 * This function is used to feed the SLCH frame data then demuxer will check if there is video/audio/data stream in it.
 *    If demuxer found video/audio/data stream, the associated callback function will be called by demuxer.
 *
 * \param data	[IN] SLCH frame data;
 * \param len	[IN] SLCH frame length;
 *
 * \return   
 *    MXD_RTN_OK	-  demux ok.
 *
 * \warning    
 */
MXD_RETURN_CODE DMX_FeedServiceFrameData( MXD_HANDLE hDevice, MXD_UINT32 nServiceID, MXD_UINT8* data, MXD_INT len );

/*Private structs*/

typedef struct sEmergencyMsgDataSaver
{
	MXD_UINT32 m_nMsgId;
	MXD_UINT32* m_pMsgSectionBodyLenArray;
	MXD_UINT8** m_pMsgSectionBodyArray;
	MXD_UINT8 m_nTotalSection;
	struct sEmergencyMsgDataSaver* next;
	MXD_UINT8* m_pTotalBuf;
}EMGSAVER;

typedef struct sEmergencyMsgStorage
{
	EMGSAVER* m_pEmergencyMsgDataHeader;
	EMGSAVER* m_pEmergencyMsgDataTail;
}EMGSTG;

typedef struct sEsgBlockSaver
{
	MXD_UINT8 m_nBlockId;
	MXD_UINT8 m_nBlockTypeId;
	MXD_BOOL m_bZipBlock;
	MXD_UINT32* m_pEsgBlockBodyLenArray;
	MXD_UINT8** m_pEsgBlockBodyArray;
	MXD_UINT8 m_nTotalBlock;
	struct sEsgBlockSaver* next;
	MXD_UINT8* m_pTotalBuf;
}ESGSAVER;

typedef struct sEsgBlockStorage
{
	struct sEsgBlockSaver* m_pEsgBlockDataHeader;
	struct sEsgBlockSaver* m_pEsgBlockDataTail;
}ESGSTG;

typedef struct sDemuxerContext
{
	MXD_CMMBSERVICE_INFO* m_pSeviceChannelList;
	MXD_UINT32 m_nServiceCount;
	void** m_pUserParams;
	MXD_UINT8* m_pMediaBuffer;
	MXD_UINT32 m_nMediaBufferLen;
	EMGSTG m_sEmergencyMsgStorage;
	MXD_ESG_DATA m_sEsgDataCtn;
	ESGSTG m_sEsgBlockStorage;
	MXD_EADT_INFO* m_pEadtInfoList;
	MXD_UINT8 m_nEadtInfoCount;
	MXD_SYS_NIT_CONF_T m_sNitInfo;
	MXD_UINT8* m_pEsgDataTypeIDs;
	MXD_UINT8 m_nEsgDataTypeIDCount;
	MXD_UINT8 m_nWantEsgDataTypeID;
}MXD_DEMUXER_CONTEXT;

/*Private structs end*/

#ifdef __cplusplus
}
#endif

#endif /* __MXD_CMMB_DEMUXER_H_ */

/* end of MxdCmmbDemuxer.h */

